(function ($) {
	$.DHCSelectBoard = $.Class.extend({
		init:function(scrollId1,scrollId2,type,checkMenu,block){
			var self = this;
			self.scrollId1 = scrollId1;
			self.scrollId2 = scrollId2;
			var keyborad = document.body.querySelector('.multiPopover');
			if (keyborad) {
				return;
			}
			var keyborad = document.createElement('div');
			keyborad.setAttribute('class','mui-popover multiPopover');
			document.body.appendChild(keyborad);
			var menuHtml = getMenuHtml(type,checkMenu);
			keyborad.innerHTML = '<div class="rights">All Rights Reserved ©东华医为</div>'+
				'<div class="mulClose nurfont nur-error"></div>'+
				'<div class="mui-scroll-wrapper">'+
					'<div class="mui-scroll"><ul class="mui-table-view">'+menuHtml+'</ul></div>'+
				'</div>'+
				'<div class="mulRight">'+
					'<div class="mulMark">备注</div>'+
					'<div class="mulUp mui-icon mui-icon-pulldown"></div>'+
					'<div class="mulDown mui-icon mui-icon-pulldown"></div>'+
				'</div>';
			if (type == 'A') {//是否模式
				var lis = keyborad.querySelectorAll('li');
				for(var i = 0; i < lis.length; i++) {
					lis[i].addEventListener('tap', function() {
						var newScore = this.getAttribute('data-key');
						block('add',newScore);
					}, false);
				}
			}
			var mark = keyborad.querySelector('.mulMark');
			mark.addEventListener('tap',function(){
				block('remark');
			});
			var last = keyborad.querySelector('.mulUp');
			last.addEventListener('tap',function(){
				block('last');
			});
			var next = keyborad.querySelector('.mulDown');
			next.addEventListener('tap',function(){
				block('next');
			});
			var mulClose = keyborad.querySelector('.mulClose');
			mulClose.addEventListener('tap',function(){
				self.hideDHCKeyboard();
				block('close');
			});
			mui('.mui-scroll-wrapper').scroll();
		},
		updateDHCKeyboardUI:function(type,checkMenu,currScore,block){
			var tableView = document.body.querySelector('.multiPopover .mui-table-view');
			var html = getMenuHtml(type,checkMenu);
			tableView.innerHTML = html;
			var scores = currScore.split('」')
			for (var i= 0; i < scores.length;i++) {
				var newActive = tableView.querySelector('li[data-key="'+scores[i]+'"]')
				if (newActive) {
					newActive.classList.add('dhc-active')
					var checkDom = newActive.querySelector('input');
					if (checkDom) {
						checkDom.checked = true;
					}
				}
			}
			
			var keyborad = document.body.querySelector('.multiPopover');
			var lis = keyborad.querySelectorAll('li');
			for(var i = 0; i < lis.length; i++) {
				if (type == 'E') {//A是否模式,E选项模式,F扣分原因模式(多选)
					lis[i].addEventListener('tap', function() {
						var oldActive = this.parentNode.querySelector('.dhc-active')
						if (oldActive) {
							oldActive.classList.remove('dhc-active')
						}
						this.classList.add('dhc-active')
						var newScore = this.getAttribute('data-key');
						block(newScore);
					}, false);
				}else if (type == 'F') {
					lis[i].addEventListener('tap', function() {
						var checkDom = this.querySelector('input');
						if (this.classList.contains('dhc-active')) {
							this.classList.remove('dhc-active')
						}else{
							this.classList.add('dhc-active')
						}
						checkDom.checked = !checkDom.checked
						var tableView = document.body.querySelector('.multiPopover .mui-table-view');
						var doms = tableView.querySelectorAll('li.dhc-active')
						var newScore = '',inputStr = '';
						for (var j = 0; j < doms.length;j++) {
							if (newScore != '') {
								newScore += '」';
								inputStr += ',';
							}
							newScore += doms[j].getAttribute('data-key')
							inputStr += doms[j].getAttribute('data-text');
						}
						block(newScore,inputStr);
					}, false);
				}
			}
			jQuery("input[type='checkbox']").click(function(){
   				this.checked = !this.checked;
   			});
			mui('.multiPopover .mui-scroll-wrapper').scroll().scrollTo(0, 0, 0);
		},
		showDHCKeyboard:function(){
			var scroll1 = document.getElementById(this.scrollId1);
			scroll1.style.paddingBottom = 252*SCALE + 30 + 'px'; //多写一个cell 的行高 不抖动
			var keyborad = document.body.querySelector('.multiPopover');
			keyborad.classList.add('mui-active');
			keyborad.style.display = 'block';
		},
		hideDHCKeyboard:function(){
			var keyborad = document.body.querySelector('.multiPopover');
			keyborad.classList.remove('mui-active');
			keyborad.style.display = 'none';
			var scroll1 = document.getElementById(this.scrollId1);
			scroll1.style.paddingBottom = '30px';
			mui('#'+scroll1.parentNode.id).scroll().reLayout();
		},
		openDHCKeyboardDrag:function(){
			var keyborad = document.body.querySelector('.multiPopover');
			var dragDom = keyborad.querySelector('.rights')
			//限制最大宽高，不让滑块出去
			var maxW = screen.width;
			var maxH = screen.height;
			if (maxH > maxW) {
				maxH = screen.width;
				maxW = screen.height;
			}
			if (mui.os.android) {
				maxH -= 335
			}else{
				maxH -= 320;
			}
			maxW -= 280;
			//手指触摸开始，记录div的初始位置
			dragDom.addEventListener('touchstart', function(e) {
				var ev = e || window.event;
			 	var touch = ev.targetTouches[0];
			 	oL = touch.clientX - keyborad.offsetLeft;
			 	oT = touch.clientY - keyborad.offsetTop;
			 	document.addEventListener("touchmove", defaultEvent, false);
			});
			//触摸中的，位置记录
			dragDom.addEventListener('touchmove', function(e) {
			 	var ev = e || window.event;
			 	var touch = ev.targetTouches[0];
			 	var oLeft = touch.clientX - oL;
			 	var oTop = touch.clientY - oT;
			 	if(oLeft < 0) {
				 	oLeft = 0;
			 	}else if (oLeft > maxW) {
			 		oLeft = maxW;
			 	}
			 	if(oTop < 0) {
			 		oTop = 0;
			 	}else if (oTop > maxH) {
			 		oTop = maxH;
			 	}
			 	keyborad.style.left = oLeft + 'px';
			 	keyborad.style.top = oTop + 'px';
			});
			//触摸结束时的处理
			dragDom.addEventListener('touchend', function() {
 				document.removeEventListener("touchmove", defaultEvent);
			});
			//阻止默认事件
			function defaultEvent(e) {
			 	e.preventDefault();
			}
		}
	});
	function getMenuHtml(type,checkMenu){
		if (!checkMenu || checkMenu.length == 0) {
			return '<div class="wrapperNull">没有选项</div>';
		}
		var html = '';
		checkMenu = checkMenu.split('「')
		for (var i = 0; i < checkMenu.length;i++) {
			var arr = checkMenu[i].split('」');
			if (type == 'A') {// 是」√」el-icon-circle-check「否」×」el-icon-circle-close「不适用」-」el-icon-warning-outline
				html +='<li class="mui-table-view-cell" data-key="' + arr[1] + '">' + arr[0] + '</li>'
			}else if (type == 'E') {//选项模式
				html +='<li class="mui-table-view-cell" data-key="'+arr[1]+'">'+arr[0]+'（'+arr[1]+'分）</li>'
			}else if (type == 'F') {//扣分原因模式--多选
				html +='<li class="mui-table-view-cell" data-key="'+i+'" data-text="'+arr[0]+'">'+
					'<input type="checkbox" /><label>'+arr[0]+'（' +arr[1]+ '分）</label></li>';
			}
		}
		return html;
	}
})(mui)